package com.bdqn.controller;


import com.bdqn.entity.DataModel;
import com.bdqn.entity.Parameters;
import com.bdqn.entity.SaleGoods;
import com.bdqn.service.SaleGoodsManageService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@RestController
@CrossOrigin

public class SaleGoodsManageController {
    @Autowired
    SaleGoodsManageService service;

    @RequestMapping(value = "SaleGoods")
    public DataModel<SaleGoods> getSaleGoods(Parameters parameters) {
        parameters.setPage((parameters.getPage() - 1) * parameters.getLimit());
        DataModel<SaleGoods> dataModel = new DataModel<>();
        dataModel.setCode(0);
        dataModel.setMsg("");
        dataModel.setCount(service.getCount(parameters));
        dataModel.setData(service.getSaleGoodsInfo(parameters));
        return dataModel;
    }

    @RequestMapping(value = "addSaleGoods")
    public int addSaleGoods(SaleGoods saleGoods) {
        return service.addSaleGoods(saleGoods);
    }

    @RequestMapping(value = "saleGoodsExport")
    public boolean CreateTable(HttpServletResponse response, Parameters parameters) {
        parameters.setPage((parameters.getPage() - 1) * parameters.getLimit());
        try (Workbook workbook = new XSSFWorkbook();
//             FileOutputStream fileOut = new FileOutputStream("E:\\sale.xlsx")
             ServletOutputStream fileOut = response.getOutputStream();

        ) {
            response.setHeader("Content-Disposition", "attachment;fileName=sale.xls");
            Sheet sheet = workbook.createSheet("sale_goods");
            parameters.setLimit(99999999);
            List<SaleGoods> list = service.getSaleGoodsInfo(parameters);

            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("序号");
            headerRow.createCell(1).setCellValue("部门");
            headerRow.createCell(2).setCellValue("操作人");
            headerRow.createCell(3).setCellValue("客户名称");
            headerRow.createCell(4).setCellValue("商品名称");
            headerRow.createCell(5).setCellValue("销售数量");
            headerRow.createCell(6).setCellValue("销售单价");
            headerRow.createCell(7).setCellValue("销售金额");
            headerRow.createCell(8).setCellValue("销售时间");
            headerRow.createCell(9).setCellValue("毛利润");
            headerRow.createCell(10).setCellValue("毛利率");

            int indexRow = 0;
            for (SaleGoods goods : list) {
                Row row = sheet.createRow(++indexRow);
                row.createCell(0).setCellValue(indexRow);
                row.createCell(1).setCellValue(goods.getDepartmentName());
                row.createCell(2).setCellValue(goods.getStaffName());
                row.createCell(3).setCellValue(goods.getCustomName());
                row.createCell(4).setCellValue(goods.getCommodityName());
                row.createCell(5).setCellValue(goods.getNumber());
                row.createCell(6).setCellValue(goods.getPrice());
                row.createCell(7).setCellValue(goods.getTotalPrice());
                row.createCell(8).setCellValue(goods.getCurrDate());
                row.createCell(9).setCellValue(goods.getProfit());
                row.createCell(10).setCellValue(goods.getInterest_rate());
            }
            workbook.write(fileOut); // 写入文件


        }catch (IOException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
